package com.lzw.service.impl.superAdmin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lzw.mapper.superAdmin.AdminManageMapper;
import com.lzw.pojo.Admins;
import com.lzw.pojo.SuperAdmins;
import com.lzw.service.superAdmin.AdminManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminManageServiceImpl implements AdminManageService {

    @Autowired
    private AdminManageMapper adminManageMapper;

    @Override
    public List<Admins> list(Integer pageNum, Integer pageSize, String name, String phone) {
        PageHelper.startPage(pageNum,pageSize);
        List<Admins> list = adminManageMapper.list(name,phone);
        Page<Admins> Admins = (Page<Admins>) list;
        return Admins;
    }

    @Override
    public void delete(List<Integer> ids) {
        adminManageMapper.delete(ids);
    }

    @Override
    public void add(Admins admins) {
        if (adminManageMapper.countByName(admins.getName()) > 0) {
            throw new IllegalArgumentException("管理员姓名已存在");
        }
        if (adminManageMapper.countByUsername(admins.getUsername()) > 0) {
            throw new IllegalArgumentException("用户名已存在");
        }
        if (adminManageMapper.countByPhone(admins.getPhone()) > 0) {
            throw new IllegalArgumentException("联系电话已存在");
        }
        adminManageMapper.add(admins);
    }

    @Override
    public void update(Admins admins) {
        Admins existing = adminManageMapper.getAdminById(admins.getAdminId());

        //检查学生是否存在
        if (existing == null){
            throw new IllegalArgumentException("系统管理员不存在");
        }

        // 检查姓名是否修改
        if (!existing.getName().equals(admins.getName())
                && adminManageMapper.countByName(admins.getName()) > 0) {
            throw new IllegalArgumentException("管理员姓名已存在");
        }

        // 检查用户名是否修改
        if (!existing.getUsername().equals(admins.getUsername())
                && adminManageMapper.countByUsername(admins.getUsername()) > 0) {
            throw new IllegalArgumentException("用户名已存在");
        }

        // 检查电话是否修改
        if (!existing.getPhone().equals(admins.getPhone())
                && adminManageMapper.countByPhone(admins.getPhone()) > 0) {
            throw new IllegalArgumentException("联系电话已存在");
        }

        adminManageMapper.update(admins);
    }
}
